using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class Player : MonoBehaviour
{
    public Rigidbody2D rigidbodyBird;
    public float fous;

    private bool death = false;

    public Animator animator;

    public delegate void DeathNotify();

    public event DeathNotify OnDeath;

    public UnityAction<int> OnScore;

    private Vector3 initPos;

    // Start is called before the first frame update
    void Start()
    {
        this.animator = this.GetComponent<Animator>();
        this.Idle();
    }


    public void Init()
    {
        this.transform.position = initPos;
        this.Idle();
        this.death = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (this.death)
        {
            return;
        }

        if (Input.GetMouseButtonDown(0))
        {
            rigidbodyBird.velocity = Vector2.zero;
            rigidbodyBird.AddForce(new Vector2(0, fous*10f));
        }
    }
    public void Idle()
    {
        this.rigidbodyBird.simulated = false;
        this.animator.SetTrigger("idle");
    }

    public void Fly()
    {
        this.rigidbodyBird.simulated = true;
        this.animator.SetTrigger("fly");
    }

    public void die()
    {
        death = true;
        if (this.OnDeath != null)
        {
            this.OnDeath();
        }
    }

    public void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.name.Equals("ScoreArea"))
        {
            if (this.OnScore != null)
                this.OnScore(1);
        }else
        this.die();
    }

    public void OnCollisionEnter2D(Collision2D collision)
    {
        this.die();
    }

}
